package com.erms.util.office;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import net.sf.json.JSONArray;

import org.apache.poi.hslf.model.Notes;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;

import com.erms.util.StringUtil;

public class MSPowerPoint extends MSOffice {

	public MSPowerPoint(String inputFileName) {

		super(inputFileName);
	}

	public MSPowerPoint(String inputFileName, String encoding) {

		super(inputFileName, encoding);
	}

	public List<Map<String, String>> toJpg() throws Exception {

		if (this.inputFile == null) { return null; }
		FileInputStream is = null;
		is = new FileInputStream(this.inputFile);

		if (this.inputFile.endsWith(".ppt")) {
			return toJpgWithPpt(is);
		} else if (this.inputFile.endsWith(".pptx")) {
			return toJpgWithPptx(is);
		} else {
			logger.debug("不支持的ppt格式");
		}
		if (is != null) {
			is.close();
		}
		return null;
	}

	
	
	private List<Map<String, String>> toJpgWithPpt(InputStream is) throws Exception {

		SlideShow ppt = new SlideShow(is);
		is.close();

		Dimension pageSize = ppt.getPageSize();
		Slide[] slides = ppt.getSlides();
		String path = "/ppt/" + getFileName();
		File parentPath = new File(rootPath + path);
		if (!parentPath.exists()) {
			logger.debug("当前文件夹不存在，新建文件夹："+parentPath);
			parentPath.mkdirs();
		}else {
			/*logger.debug("已存在该文件夹,表明可能已经解析过该文件，尝试查找里面的信息文件");
			String result = readSavedInfo(rootPath + path);
			if (result != null) {
				JSONArray.t
			}*/
		}
		List<Map<String, String>> imgList = new ArrayList<>();
		// 处理字体
		int n = slides.length > 50 ? 50 : slides.length;
		for (int i = 0; i < n; i++) {
			Slide slide = slides[i];
			TextRun[] runs = slide.getTextRuns();
			for (int j = 0; j < runs.length; j++) {
				RichTextRun[] richTextRuns = runs[j].getRichTextRuns();
				for (int k = 0; k < richTextRuns.length; k++) {
					RichTextRun richTextRun = richTextRuns[k];
					String fontName = richTextRun.getFontName();
					//替换易导致乱码的truetype字体
					if (isTrueType(fontName)) {
						richTextRun.setFontName("宋体");
					} else {
						//
					}
				}

			}

			BufferedImage img = new BufferedImage(pageSize.width,
					pageSize.height, BufferedImage.TYPE_INT_RGB);

			Graphics2D graphics = img.createGraphics();
			graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			graphics.setRenderingHint(RenderingHints.KEY_RENDERING,
					RenderingHints.VALUE_RENDER_QUALITY);
			graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
					RenderingHints.VALUE_INTERPOLATION_BICUBIC);
			graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
					RenderingHints.VALUE_FRACTIONALMETRICS_ON);

			graphics.setColor(Color.white);
			graphics.clearRect(0, 0, pageSize.width, pageSize.height);

			slide.draw(graphics);

			String note = "";
			Notes notes = slide.getNotesSheet();
			if (notes != null) {
				TextRun[] textRuns = notes.getTextRuns();
				for (int j = 0; j < textRuns.length; j++) {
					TextRun textRun = textRuns[j];
					note += textRun.getText();								
				}
			}
			
			//TODO 这里需要替换图片的路径生成规则
			String imgName = rootPath + path + "/image" + i + ".jpg";
			
			FileOutputStream out = new FileOutputStream(imgName);
			ImageIO.write(img, "JPEG", out);
			out.close();
			logger.debug("ppt标题："+ slide.getTitle());
			logger.debug("ppt备注："+ note);	
			Map<String, String> map = new HashMap<String, String>();
			map.put("title", slide.getTitle());
			map.put("des", note);
			map.put("img", "temp"+path + "/image" + i + ".jpg");
			imgList.add(map);
		}
		return imgList;
	}

	private List<Map<String, String>> toJpgWithPptx(InputStream is) throws Exception {

		return null;
	}
}
